home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7977 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  97 lines

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple Program Question
  5. Date: 29 Feb 1996 18:17:03 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4h5c5f$fhb@umbc9.umbc.edu>
  8. References: <4gsr9u$sk6@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Tycope <tycope@aol.com> wrote:
  13. |> I am trying to write a non -interactive program that calculates all
  14. |> integer triples (i, j, k) such that 
  15. |> 0 < i < j < k < l and i + j + k = l. Print out the number of triples that
  16. |> satisfy the requirements and print out every millionth triple.  Can anyone
  17. |> see where I am missing the boat in the following code.  The program runs
  18. |> and immediately terminates.  Thanks in advance for any feedback. 
  19. |> 
  20. |> #include <stdio.h>
  21. |> 
  22. |> long int i, j, k, l;
  23. |> long int count;
  24.  
  25. Ok we can assume all these are 0 since they are declared global.
  26.  
  27. |> int
  28. |> main (void)
  29. |> {
  30. |>     do
  31. |>     {
  32. |>         (l = i + j + k);
  33.  
  34. This is zero the first time through the loop and 9 (2+3+4) every time after
  35. that.
  36.  
  37. |>         (i = 1);
  38. |>         (j = 2);
  39. |>         (k = 3);
  40.  
  41. These values are set EVERY iteration.
  42.  
  43. |>         (i++, j++, k++);
  44.  
  45. That makes your new triple (2,3,4), but not for long...See above.
  46.  
  47. |>         (++count);
  48. |>     if (count % 1000000 == 0)
  49.  
  50. Personally I'd use some extra ()'s there.
  51.  
  52. |>      {
  53. |>         printf("%ld(i) + %ld(j) + %ld(k) = %ld(l)", i, j, k, l);
  54.  
  55. Either make a newline or stick in an explicit fflush() or you may be
  56. sitting around for output.
  57.  
  58. |>      }
  59. |> 
  60. |>     } while (0 < i < j < k < l);
  61.  
  62. Now this is flat out wrong. C boolean operators are not mathematics
  63. inequalities so you must explicitly say what you want. In this case:
  64.  
  65. while ((0 < i) && (i < j) && (j < k) && (k < l));
  66.  
  67. |> 
  68. |>     return (0);
  69. |> }
  70.  
  71. Here's some fixed code using a for loop to compute the same:
  72.  
  73. #include <stdio.h>
  74.  
  75. int main (void)
  76. {
  77.   long int i, j, k, l;
  78.   long int count;
  79.  
  80.   for (i = 1, j = i + 1, k = j + 1, l = i + j + k, count = 0;
  81.        (0 < i) && (i < j) && (j < k) && (k < l);
  82.        i++, j++, k++, count++, l = i + j + k)
  83.       if ((count % 1000000) == 0)
  84.          printf ("%ld(i) + %ld(j) + %ld(k) = %ld(l)\n", i, j, k, l);
  85.  
  86.   return (0);
  87. }
  88.  
  89. Note that eventually something is going to overflow so there should be
  90. an extra terminating condition based on the number of iterations you
  91. want to perform (MAXIMUM) in your program. The above code should serve
  92. as only a suggestion on a possible way to begin.
  93. -- 
  94. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  95.  
  96. Jonas J. Schlein  (schlein@gl.umbc.edu)
  97.